<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sina_crawl extends CI_Controller {

	/**
	* __construct 
	*
	* @access public
	* @return string $mixed
	*/
	public function __construct() {
        parent::__construct();
		ini_set("display_errors","on");
		error_reporting(E_ALL & ~E_NOTICE);
		set_time_limit(0);
		
		$this->load->library('error_code_library');
	}


	/**
	* cookie_test
	*
	* @access public
	* @return string $mixed
	*/
	public function cookie_test() {
		$this->load->helper('form');
		$this->load->view('cookie_test_sina');
	}

    function unlocck() {
        $this->load->library('unlock_sina_library');
        $this->benchmark->mark('a');
        $c = $this->unlock_sina_library->get_yzm_by_dama2('verifycode.jpg');
        $this->benchmark->mark('aa');
        echo $this->benchmark->elapsed_time('a', 'aa');
        var_dump($c);
    }

    public function crawl_user_info($uid = 1907542963) {
    	header("Content-type:text/html; charset=utf-8");
    
    	$_POST['uid'] = $uid;
    	$_POST['platform'] = 'sina';
    	$_POST['soften_admin_debug'] = false;
    
    
    	//$this->load->database();
    	$this->load->library('crawl_abstract');
    	$this->load->library('crawl_keyword_library');
    	$this->load->library('Crawl_sina_user_info_from_phone_library');
    
    	$crawl_result = $this->crawl_sina_user_info_from_phone_library->crawl(false);
    	echo "<pre>";
    	print_r($crawl_result);
    	echo "</pre>";
    }

	/**
	* crawl_keyword_from_html
	*
	* @access public
	* @return string $mixed
	*/
	public function crawl_keyword_from_html($page = 1) {
		
		header("Content-type:text/html; charset=utf-8");
		
		$_POST['word'] = '汉能';
		$_POST['page'] = $page;
		$_POST['nopage_limit'] = "yes";
		$_POST['platform'] = 'sina';
		//$_POST['timescope'] = 'custom:2014-11-01-0:2014-11-01-3';
		$_POST['soften_admin_debug'] = true;


		$this->load->database();
        $this->load->library('crawl_abstract');
        $this->load->library('crawl_keyword_library');
	    $this->load->library('Crawl_sina_keyword_from_html_library');

	    $crawl_result = $this->crawl_sina_keyword_from_html_library->crawl(true);
	    echo "<pre>";
		print_r($crawl_result);
		echo "</pre>";
	}

	/**
	* crawl_keyword_from_html
	*
	* @access public
	* @return string $mixed
	*/
	public function crawl_keyword_from_wap($page = 1) {
		header("Content-type:text/html; charset=utf-8");
		
		$_POST['word'] = '旅游 投诉';
		$_POST['page'] = $page;
		$_POST['platform'] = 'sina';
		$_POST['soften_admin_debug'] = true;


		$this->load->database();
        $this->load->library('crawl_abstract');
        $this->load->library('crawl_keyword_library');
	    $this->load->library('Crawl_sina_keyword_from_phone_library');

	    $crawl_result = $this->crawl_sina_keyword_from_phone_library->crawl(true);
        echo "<pre>";
        print_r($crawl_result);
        echo "</pre>";
	}
	
	public function crawl_user_from_wap($page = 1) {
		header("Content-type:text/html; charset=utf-8");
	
		$_POST['uid'] = '1907542963';
		$_POST['page'] = $page;
		$_POST['platform'] = 'sina';
		$_POST['soften_admin_debug'] = false;
	
	
		$this->load->database();
		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('Crawl_sina_user_from_phone_library');
	
		$crawl_result = $this->crawl_sina_user_from_phone_library->crawl(false);
		echo "<pre>";
		print_r($crawl_result);
		echo "</pre>";
	}

	function crawl_sina_content_from_phone_test() {
		$_POST['mid'] = '4074854170720230';
		$this->load->database();
		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('crawl_sina_from_phone_json_library');
		$this->load->library('crawl_sina_content_from_phone_json_library');
		$rs = $this->crawl_sina_content_from_phone_json_library->crawl();
		print_r($rs);
	}
	
	function crawl_sina_info_test() {
		$_POST['platform'] = 'sina';
		
		$str = 'F4zmdespQ';
		$this->load->database();
		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('Crawl_sina_info_from_phone_library');
		$rs = $this->crawl_sina_info_from_phone_library->crawl(false, $str);
		print_r($rs);
	}

	function crawl_sina_author_from_phone_test() {
		$_POST['uid'] = '6034761912';
		$author = array();
		$this->load->database();
		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('crawl_sina_from_phone_json_library');
		$this->load->library('crawl_sina_author_from_phone_json_library');
		$rs = $this->crawl_sina_author_from_phone_json_library->crawl();
		if(isset($rs->user)) {
			$user = $rs->user;
			$author['followers_count'] = $user->followers_count;
			$author['friends_count'] = $user->follow_count;
			$author['gender'] = $user->gender;
			$author['name'] = $user->screen_name;
			$author['verified'] = $user->verified;
			$author['verified_reason'] = $user->verified_reason;
			$author['profile_url'] = $user->profile_url;
			$author['avator_img'] = $user->profile_image_url;
			$author['location'] = '';
		}
		$this->load->library('crawl_sina_author_location_from_phone_json_library');
		$rs = $this->crawl_sina_author_location_from_phone_json_library->crawl();
		if($rs) {
			$author['location'] = $rs;
		}
		print_r($author);
	}

	function crawl_sina_comments_from_phone_test() {
		$url = 'http://weibo.com/2834256503/ECOhpkHfv?from=page_1005052834256503_profile&wvr=6&mod=weibotime&type=comment#_rnd1491358632308';
		$url_setments = parse_url($url);
		$url = 'http://m.weibo.cn/'.$url_setments['path'];

		$content = file_get_contents($url);
		if(empty($content)) {
			echo  '未获取到微博内容';
		}
		if(preg_match('/"mid": "(\d*)"/ims', $content, $match)) {
			$_POST['mid'] = $match[1];
		}
		$_POST['page'] = 2;
		$this->load->library('crawl_abstract');
		$this->load->library('crawl_keyword_library');
		$this->load->library('crawl_sina_from_phone_json_library');
		$this->load->library('crawl_sina_comments_from_phone_json_library');
		$rs = $this->crawl_sina_comments_from_phone_json_library->crawl();
		print_r($rs);
	}
	
	public function get_cookie() {
		$this->load->database();
	
		$this->load->model('keyword/Cookie_model');
		$cookie = $this->Cookie_model->get_cookie("sina");
		print_r($cookie);
	}


    public function crawl_keyword_from_phone($page = 1) {
		
    	header("Content-type:text/html; charset=utf-8");
        $_POST['word'] = '经济';
        $_POST['page'] = $page;
        $_POST['platform'] = 'sina';
        //$_POST['soften_admin_debug'] = true;


        $this->load->database();
        $this->load->library('crawl_abstract');
        $this->load->library('crawl_keyword_library');
        $this->load->library('Crawl_sina_keyword_from_phone_library');

        $crawl_result = $this->crawl_sina_keyword_from_phone_library->crawl(FALSE);
        echo "<pre>";
        print_r($crawl_result);
        echo "</pre>";
    }
    
    public function crawl_sina_hot_from_html($page = 1) {
    
    	header("Content-type:text/html; charset=utf-8");
    	
    	$_POST['cat'] = '4888';
    	$_POST['page'] = $page;
    	$_POST['platform'] = 'sina';
    	//$_POST['soften_admin_debug'] = true;
	
	$_POST['dateType']='9999' ;
    
    
    	$this->load->database();
    	$this->load->library('crawl_abstract');
    	$this->load->library('crawl_keyword_library');
    	$this->load->library('crawl_sina_hot_from_html_library');

    	$crawl_result = $this->crawl_sina_hot_from_html_library->crawl(FALSE);
    	echo "<pre>";
    	print_r($crawl_result);
    	echo "</pre>";
    }

    
    public function test($page = 1) {
    	$this->load->helper('url');
 
    	$post_data['word'] = '宝马';
    	$post_data['page'] = $page;
    	$post_data['html'] = "yes";
    	$post_data['nopage_limit'] = "yes";
    	$post_data['platform'] = 'sina';
    	$post_data['timescope'] = 'custom:2014-11-01-0:2014-11-01-3';
    	$post_data['method'] = "POST";
    	$post_data['token'] = "9f7ffa5b38e13b1843eeffcc3ba06565";
    	
    	$post_data['soften'] = "yes";
    	//$post_data['soften_admin_debug'] = true;
    
    	$url= site_url("keyword/index");
    
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_URL,$url);
    
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    	$result = curl_exec($ch);
    	if ($result === FALSE) {
    		echo "CURL Error In Test Connect API: " . curl_error($ch). " TO ".$url;
    	}
    	curl_close($ch);
    
    	print_r($result);
    
    }
}
